<?php

namespace Forms;

use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Text,
    Phalcon\Forms\Element\TextArea,
    Phalcon\Forms\Element\File,
    Phalcon\Forms\Element\Select,
    Phalcon\Forms\Element\Hidden,
    Phalcon\Forms\Element\Submit,
    Phalcon\Validation\Validator\PresenceOf,
    Phalcon\Validation\Validator\Identical;
use Models\Doctypes;
use Phalcon\Mvc\Model\Message as ModelMessage;
use Phalcon\Validation\Message;
use Phalcon\Validation\Message\Group;

/**
 * validate and print render Frontend login form
 */
class UploadForm extends Form {

    public function initialize($options = null) {

        $doctpyes = new Select('doctype', Doctypes::find(), array(
            'using' => array('id', 'name'),
            'useEmpty' => true,
            'emptyText' => 'Chọn loại tài liệu',
            'emptyValue' => "",
        ));
        $doctpyes->addValidators(array(
            new PresenceOf(array(
                'message' => 'Vui lòng chọn loại tài liệu'
                    ))
        ));

        $this->add($doctpyes);

        if (isset($options) && isset($options->doctype)) {
            $topics = new Select('topic', \Models\Topics::find('doctype_id=\'' . $options->doctype . '\''), array(
                'using' => array('id', 'name'),
                'useEmpty' => true,
                'emptyText' => 'Chọn đề tài',
                'emptyValue' => 0,
            ));
        } else {
            $topics = new Select('topic', array(), array(
                'using' => array('key', 'value'),
                'useEmpty' => true,
                'emptyText' => 'Chọn đề tài',
                'emptyValue' => 0,
            ));
        }

        $this->add($topics);

        $title = new Text('title');
        $title->addValidators(array(
            new PresenceOf(array(
                'message' => 'Vui lòng nhập tiêu đề'
                    ))
        ));

        $this->add($title);

        $image = new File('image');
        $image->addValidators(array(
            new PresenceOf(array(
                'message' => 'Bạn chưa chọn file ảnh'
                    ))
        ));


        $this->add($image);

        $file = new File('file');
        $file->addValidators(array(
            new PresenceOf(array(
                'message' => 'Bạn chưa chọn file upload'
                    ))
        ));

        $this->add($file);

        $cost = new Text('cost');
        $cost->setDefault('0');
        $cost->addValidators(array(
            new PresenceOf(array(
                'message' => 'The cost is required'
                    ))
        ));
        $this->add($cost);

        $description = new TextArea('description');
        $this->add($description);

        //CSRF
        $csrf = new Hidden('csrf');

        $csrf->addValidator(
                new Identical(array(
            'value' => $this->security->getSessionToken(),
            'message' => 'CSRF validation failed'
                ))
        );

        $this->add($csrf);

        $this->add(new Submit('go', array(
            'class' => 'btn btn-primary btn-large'
        )));
    }

    /**
     * Prints messages for a specific element
     */
    public function messages($name) {
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                $this->flash->error($message);
            }
        }
    }

    public function appendMessage($message, $field, $type = null) {
        if (is_string($message)) {
            $message = new Message($message, $field, $type);
        }

        if ($message instanceof Message || $message instanceof ModelMessage) {
            // Check if there is a group for the field already.
            if (!is_null($this->_messages) && array_key_exists($field, $this->_messages)) {
                $this->_messages[$field]->appendMessage($message);
            } else {
                $this->_messages[$field] = new Group(array($message));
            }
        } else {
            throw new Exception("Can't append message into the form, invalid type.");
        }
    }

}
